<script setup lang="ts">
import { ref, watch } from 'vue';
import { useFormRules, useNaiveForm } from '@/hooks/common/form';

defineOptions({
  name: 'CodeTableConfigForm'
});

interface Props {
  /** the edit row data */
  table: Api.Model;
}

const props = defineProps<Props>();

// const formRef = ref();
const { formRef, validate, restoreValidation } = useNaiveForm();
const form = ref<Api.Model>({ id: 0 });

type RuleKey = Extract<keyof Api.Model, 'userName' | 'status'>;
const { defaultRequiredRule } = useFormRules();
const rules: Record<RuleKey, App.Global.FormRule> = {
  userName: defaultRequiredRule,
  status: defaultRequiredRule
};

/** 监听 table 属性，复制给 formData 属性 */
watch(
  () => props.table,
  table => {
    form.value = table;
  },
  {
    deep: true,
    immediate: true
  }
);

defineExpose({
  validate: async () => validate(),
  restoreValidation: async () => restoreValidation()
});
</script>

<template>
  <NForm ref="formRef" :model="form" :rules="rules">
    <NDivider title-placement="left">基本信息</NDivider>
    <NGrid responsive="screen" cols="4">
      <NFormItemGi class="pr-12px" label="后端地址" path="sourceDir">
        <NInput v-model:value="form.sourceDir" placeholder="后端地址" />
      </NFormItemGi>
      <NFormItemGi class="pr-12px" label="前端地址" path="uiSourceDir">
        <NInput v-model:value="form.uiSourceDir" placeholder="前端地址" />
      </NFormItemGi>
      <NFormItemGi class="pr-12px" label="基础包名" path="basePackage">
        <NInput v-model:value="form.basePackage" placeholder="基础包名" />
      </NFormItemGi>
      <NFormItemGi class="pr-12px" label="业务模块" path="biz">
        <NInput v-model:value="form.biz" placeholder="模块名" />
      </NFormItemGi>
      <NFormItemGi class="pr-12px" label="业务域" path="domain">
        <NInput v-model:value="form.domain" placeholder="业务域" />
      </NFormItemGi>
      <NFormItemGi class="pr-12px" label="生成场景" path="scene">
        <PorDictSelect v-model:value="form.scene" dict-code="code_scene" value-to-number />
      </NFormItemGi>
      <NFormItemGi class="pr-12px" label="表名" path="name">
        <NInput v-model:value="form.name" placeholder="表名" />
      </NFormItemGi>
      <NFormItemGi class="pr-12px" label="表名前缀" path="tablePrefix">
        <NInput v-model:value="form.tablePrefix" placeholder="表名" />
      </NFormItemGi>
      <NFormItemGi class="pr-12px" label="表描述" path="comment">
        <NInput v-model:value="form.comment" placeholder="表描述" />
      </NFormItemGi>
      <NFormItemGi class="pr-12px" label="作者" path="author">
        <NInput v-model:value="form.author" placeholder="作者" />
      </NFormItemGi>
      <NFormItemGi class="pr-12px" label="父菜单编号" path="parentMenuId">
        <PorTreeSelect
          v-model:value="form.parentMenuId"
          url="/permission/menu/tree?types=1"
          value-to-number
          clearable
        ></PorTreeSelect>
      </NFormItemGi>
      <NFormItemGi class="pr-12px" label="模板类型" path="templateType">
        <PorDictSelect v-model:value="form.templateType" dict-code="code_template_type" value-to-number />
      </NFormItemGi>
      <NFormItemGi class="pr-12px" label="开启缓存" path="enableCache">
        <PorDictRadio v-model:value="form.enableCache" dict-code="common_true_false" value-to-bool />
      </NFormItemGi>
      <NFormItemGi class="pr-12px" label="简单列表" path="enableSimpleList">
        <PorDictRadio v-model:value="form.enableSimpleList" dict-code="common_true_false" value-to-bool />
      </NFormItemGi>
      <!--
 <NFormItemGi class="pr-12px" label="选项列表" path="enableOptions">
        <PorDictRadio v-model:value="form.enableOptions" dict-code="common_true_false" value-to-bool />
      </NFormItemGi>
-->
    </NGrid>
    <div v-if="form.templateType == 15">
      <NDivider title-placement="left">子表关系</NDivider>
      <NGrid responsive="screen" cols="4">
        <NFormItemGi class="pr-12px" label="主表" path="masterTableId">
          <PorTreeSelect
            v-model:value="form.masterTableId"
            :url="`/dev/code/codeTable/simpleList?dbId=${form.dbId}`"
            value-to-number
            clearable
          ></PorTreeSelect>
        </NFormItemGi>
        <NFormItemGi class="pr-12px" label="子表连接字段" path="joinColumnId">
          <!-- <NInput v-model:value="form.joinColumn" placeholder="子表连接字段" /> -->
          <PorTreeSelect
            v-model:value="form.joinColumnId"
            :url="`/dev/code/codeColumn/simpleList?tableId=${form.id}`"
            value-to-number
            clearable
          ></PorTreeSelect>
        </NFormItemGi>
        <NFormItemGi class="pr-12px" label="主子表关系" path="joinRelation">
          <PorDictRadio v-model:value="form.joinRelation" dict-code="code_join_relation" value-to-number />
        </NFormItemGi>
      </NGrid>
    </div>
    <div v-if="form.templateType == 2">
      <NDivider title-placement="left">树表信息</NDivider>
      <NGrid responsive="screen" cols="4">
        <NFormItemGi class="pr-12px" label="树表父字段 ID" path="parentIdColumnId">
          <PorTreeSelect
            v-model:value="form.parentIdColumnId"
            :url="`/dev/code/codeColumn/simpleList?tableId=${form.id}`"
            value-to-number
            clearable
          ></PorTreeSelect>
        </NFormItemGi>
        <NFormItemGi class="pr-12px" label="树表父字段名称" path="parentNameColumnId">
          <PorTreeSelect
            v-model:value="form.parentNameColumnId"
            :url="`/dev/code/codeColumn/simpleList?tableId=${form.id}`"
            value-to-number
            clearable
          ></PorTreeSelect>
        </NFormItemGi>
      </NGrid>
    </div>
    <NDivider title-placement="left">生成信息</NDivider>
    <NGrid responsive="screen" cols="4">
      <NFormItemGi class="pr-12px" label="覆盖生成" path="enableOverwrite">
        <PorDictRadio v-model:value="form.enableOverwrite" dict-code="common_true_false" value-to-bool />
      </NFormItemGi>
      <NFormItemGi class="pr-12px" label="生成 Controller" path="enableController">
        <PorDictRadio v-model:value="form.enableController" dict-code="common_true_false" value-to-bool />
      </NFormItemGi>
      <NFormItemGi class="pr-12px" label="生成 Service" path="enableService">
        <PorDictRadio v-model:value="form.enableService" dict-code="common_true_false" value-to-bool />
      </NFormItemGi>
      <NFormItemGi class="pr-12px" label="生成 Model" path="enableModel">
        <PorDictRadio v-model:value="form.enableModel" dict-code="common_true_false" value-to-bool />
      </NFormItemGi>
      <NFormItemGi class="pr-12px" label="生成 Vue Api" path="enableVueApi">
        <PorDictRadio v-model:value="form.enableVueApi" dict-code="common_true_false" value-to-bool />
      </NFormItemGi>
      <NFormItemGi class="pr-12px" label="生成 Vue Index" path="enableVueIndex">
        <PorDictRadio v-model:value="form.enableVueIndex" dict-code="common_true_false" value-to-bool />
      </NFormItemGi>
      <NFormItemGi class="pr-12px" label="生成 Vue Form" path="enableVueForm">
        <PorDictRadio v-model:value="form.enableVueForm" dict-code="common_true_false" value-to-bool />
      </NFormItemGi>
    </NGrid>
  </NForm>
</template>

<style scoped></style>
